using System.Web.Mvc;
using BudgetRight.Common;
using BudgetRight.Data.Adapters;
using BudgetRight.Data.Models;

namespace BudgetRight.Web.Controllers
{
    public class CategoryController : Controller
    {

        public ICategoryDbAdapter CategoryDbAdapter { get; set; }

        //
        // GET: /Category/

        public ActionResult Index()
        {
            var categories = CategoryDbAdapter.GetCategoryDescendants(1);

            return View(categories);
        }

        //
        // GET: /Category/Details/5

        public ActionResult Details(int id)
        {
            var category = CategoryDbAdapter.GetCategory(id);

            return View(category);
        }

        //
        // GET: /Category/Create

        public ActionResult Create()
        {
            ViewData["Title"] = "Create Client";

            ViewData.Model = UnityHelper.Container.Resolve<ICategoryEntity>();

            return View("Form");
        }

        //
        // POST: /Category/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View("Form");
            }
        }

        //
        // GET: /Category/Edit/5

        public ActionResult Edit(int id)
        {
            ViewData["Title"] = "Edit Client";

            var category = CategoryDbAdapter.GetCategory(id);

            return View("Form", category);
        }

        //
        // POST: /Category/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                var category = CategoryDbAdapter.GetCategory(id);

                UpdateModel(category);

                CategoryDbAdapter.Save(category);

                // TODO: Add insert logic here

                return RedirectToAction("Details", new { id = category.Id });
            }
            catch
            {
                return View("Form");
            }
        }

    }
}
